package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.ChooseAddressAdapeter;
import com.wnhz.work.yz.bean.AddressBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---选择地址
 */
public class ChooseAddressActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private ListView my_listView;
    private TextView tv_right;

    //    private List<ChooseAddressBean> chooseList = new ArrayList<>();
    private List<AddressBean> addrList = new ArrayList<>();
    private ChooseAddressAdapeter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_address);

        initData();
        initView();
    }

    private void initData() {

    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("选择地址");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.submit).setVisibility(View.VISIBLE);
        findViewById(R.id.submit).setOnClickListener(this);
        tv_right = (TextView) findViewById(R.id.submit);
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("新增");
        tv_right.setTextColor(0xff008c99);


        my_listView = (ListView) findViewById(R.id.my_listView);


        upload();
        mAdapter = new ChooseAddressAdapeter(addrList, this, this);
        my_listView.setAdapter(mAdapter);
    }

    private void upload() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        XUtil.Post(Url.MEMBER_GETADDRESSLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                super.onSuccess(result);
                System.out.println("address= " + result);
                try {
                    addrList.clear();
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
//                    if ("-1".equals(res)) {
//                        //info = "请重新登录";
//                        MyToast("请重新登录");
//                        startActivity(new Intent(ChooseAddressActivity.this, LoginNewActivity.class));
//                        return;
//                    }
                    JSONArray jsonArray = jsonObject.optJSONArray("info");

                    if (jsonArray != null) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            AddressBean bean = new AddressBean();
                            Gson gson = new Gson();
                            bean = gson.fromJson(jsonArray.optString(i), AddressBean.class);

                            addrList.add(bean);
                        }
                        for (int i = 0; i < addrList.size(); i++) {
                            if ("1".equals(addrList.get(i).getDef())) {
                                AddressBean bean = addrList.get(i);
                                addrList.add(0, bean);
                                addrList.remove(i);
                                break;

                            }
                        }
                        for (AddressBean addressBean : addrList) {
                            System.out.println("aaaaaaaaaaaaa     " + addressBean.toString());
                        }
                    }

                    mAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        Integer index;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //新增
            case R.id.submit:
//                MyToast("新增");
                intent = new Intent(this, AddAddressActivity.class);
                startActivity(intent);
                break;

            //选择
            case R.id.rl_choose_true:
                index = (Integer) v.getTag();
                addrList.get(index).setSelect(!addrList.get(index).isSelect());
                mAdapter.notifyDataSetChanged();

                intent = new Intent();
                intent.putExtra("addressBean", addrList.get(index));
                setResult(RESULT_OK, intent);
                finish();
                break;

            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        upload();
        super.onResume();
    }
}
